package geektime.spring.springbucks.mapper;

import geektime.spring.springbucks.model.Coffee;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.session.RowBounds;

import java.util.List;

@Mapper
public interface CoffeeMapper {
    @Select("select * from t_coffee order by id")
    List<Coffee> findAllWithRowBounds(RowBounds rowBounds);

    @Select({
            "<script> ",
            "select * from  t_coffee where 1=1 ",
            "<if test = 'coffee.name != null'> ",
            " and name=#{coffee.name}, ",
            "</if> ",
            "<if test = 'coffee.price != null'> ",
            "and price=#{coffee.price}, ",
            "</if> ",
            "<if test = 'coffee.ids != null and coffee.ids.size() >0'> ",
            "and id IN <foreach collection=\"coffee.ids\" item=\"value\" separator=\",\" open=\"(\" close=\")\">"+
             "#{value}</foreach> ",
            "</if> ",
            "</script>"
    })
    List<Coffee> findAllWithParam(@Param("pageNum") int pageNum,
                                  @Param("pageSize") int pageSize,
    @Param("coffee") Coffee coffee);

    @Insert({"insert into t_coffee(name, create_time,update_time,price) values(#{name}, #{createTime, jdbcType=TIMESTAMP},#{updateTime, jdbcType=TIMESTAMP},#{price})"})
    @Options(useGeneratedKeys = true, keyProperty = "id")
    long add(Coffee coffee);

    @Update({
            "<script> ",
            "update t_coffee set ",
            "<if test = 'name != null'> ",
            "name=#{name}, ",
            "</if> ",
            "<if test = 'price != null'> ",
            "price=#{price}, ",
            "</if> ",
            "update_time=CURRENT_TIMESTAMP ",
            "WHERE id=#{id}",
            "</script>"
    })
    long updateCoffeeInfo(Coffee coffee);

    @Delete("delete from t_coffee where id=#{id}")
    public int delById(@Param("id") long id);

    @Select("select * from t_coffee where id = #{id} order by id")
    Coffee getById(@Param("id") long id);
}
